package cn.yaboya.org.controller;

import cn.yaboya.basic.utils.AjaxResult;
import cn.yaboya.basic.utils.PageList;
import cn.yaboya.org.domain.Employee;
import cn.yaboya.org.query.EmployeeQuery;
import cn.yaboya.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * @Title: loadById
     * @Description: 根据一个id查询单个
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/24 10:05
     * @Parameters: [id]
     * @Return cn.yaboya.org.domain.Employee
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "id查找" )
    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    /**
     * @Title: loadAll
     * @Description: 查询全部
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/24 10:04  
     * @Parameters: []
     * @Return java.util.List<cn.yaboya.org.domain.Employee>
     */
    @GetMapping
    @ApiOperation(value = "查找全部" )
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
     * @Title:remove
     * @Description: 删除单个
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/24 10:06
     * @Parameters:
     * @Return
     */
    @DeleteMapping("{id}")
    @ApiOperation(value = "id删除" )
    public AjaxResult remove(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }
    /**
     * @Title: addOrUpt
     * @Description: 增加 或者修改接口
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/24 12:41
     * @Parameters: [employee]
     * @Return cn.yaboya.basic.utils.AjaxResult
     */
    @ApiOperation(value = "新增或者修改" )
    @PutMapping
    public AjaxResult addOrUpt(@ApiParam(name="json 对象",required = true) @RequestBody Employee employee){
        System.out.println(employee+"123=================");
        try {
            // 判断是否有id 没有就新增 有就更改
            if (employee.getId() == null){
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }

    /**
     * @Title: queryData
     * @Description: 分页+高级查询
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/24 9:51
     * @Parameters: [employeeQuery]
     * @Return cn.itsource.basic.utils.AjaxResult
     */

    @PostMapping
    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> data = employeeService.queryData(employeeQuery);
        return data;
    }



    /**
     * @Title: batchRem
     * @Description: 批量删除
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/25 11:28
     * @Parameters: [ids]
     * @Return cn.yaboya.basic.utils.AjaxResult
     */
    @PatchMapping
    public AjaxResult batchRem(@RequestBody Long[] ids){
        try {
            employeeService.batchRem(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }


}
